/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aplikasi.com.implement;

import aplikasi.com.entity.komik;
import aplikasi.com.interfac.interKomik;
import aplikasi.com.koneksiDatabase.DatabaseUtilities;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Henfryandie
 */
public class implemKomik implements interKomik {

    public komik insertKar(komik o) throws SQLException {
        String sqlInsert = "INSERT INTO aplikasirentakkomik.komik(judulKomik,pengarang,tarifSewa) VALUES (?,?,?);";
        PreparedStatement prepare = DatabaseUtilities.getConnection().prepareStatement(sqlInsert);
        prepare.setString(1, o.getJudulKomik());
        prepare.setString(2, o.getPengarang());
        prepare.setInt(3, o.getTarifSewa());
        prepare.executeUpdate();
        return o;
    }

    public List<komik> viewKomik() throws SQLException {
        Statement st = DatabaseUtilities.getConnection().createStatement();
        ResultSet rs = st.executeQuery("select judulKomik from komik");
        List<komik> list = new ArrayList<komik>();
        while (rs.next()) {
            komik o = new komik();
            o.setJudulKomik(rs.getString("judulKomik"));
            list.add(o);
        }
        return list;
    }

    public void update(komik o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void delete(String nama) throws SQLException {
        String sql = "DELETE FROM aplikasirentakkomik.komik WHERE judulKomik=?;";
        PreparedStatement prepared = DatabaseUtilities.getConnection().prepareStatement(sql);
        prepared.setString(1, nama);
        prepared.executeUpdate();
    }

    public List<komik> getAll() throws SQLException {
        Statement st = DatabaseUtilities.getConnection().createStatement();
        ResultSet rs = st.executeQuery("select * from komik");
        List<komik> list = new ArrayList<komik>();
        while (rs.next()) {
            komik o = new komik();
            o.setIdKomik(rs.getInt("idKomik"));
            o.setJudulKomik(rs.getString("judulKomik"));
            o.setPengarang(rs.getString("pengarang"));
            o.setTarifSewa(rs.getInt("tarifSewa"));
            list.add(o);
        }
        return list;
    }

    public int gettarifSewa(String nama) throws SQLException {
        int tarif = 0;
        Statement st = DatabaseUtilities.getConnection().createStatement();
        ResultSet rs = st.executeQuery("SELECT tarifSewa FROM komik WHERE judulKomik = '" + nama + "'");
        while (rs.next()) {
            komik o = new komik();
            o.setTarifSewa(rs.getInt("tarifSewa"));
            tarif = o.getTarifSewa();
        }
        return tarif;
    }

    public int getIdKomik(String nama) throws SQLException {
        int id = 0;
        Statement st = DatabaseUtilities.getConnection().createStatement();
        ResultSet rs = st.executeQuery("SELECT idKomik FROM komik WHERE judulKomik = '" + nama + "'");
        while(rs.next()){
            komik k = new komik();
            k.setIdKomik(rs.getInt("idKomik"));
            id = k.getIdKomik();
        }
        return id;
    }
}
